// import './utils/init-leancloud-sdk'
import { history } from 'umi';
export const request = {
  //中间件
  middlewares: [
    // async function middlewareA(ctx, next) {
    //   console.log('-=-=');
    //   await next();
    // },
    // async function middlewareB(ctx, next) {
    //   ctx.req.options.headers = {
    //     'X-LC-Id': 'o3FIcbLMPxolbrAeMtwB0JAN-gzGzoHsz',
    //     'X-LC-Key': 'ET9mXBeevGcW3FL6LLyY2FnX',
    //     'Content-Type': 'application/json',
    //   };
    //   await next();
    // },
  ],
  requestInterceptors: [
    (url, config) => {
      config.headers = {
        'X-LC-Id': 'o3FIcbLMPxolbrAeMtwB0JAN-gzGzoHsz',
        'X-LC-Key': 'ET9mXBeevGcW3FL6LLyY2FnX',
        'Content-Type': 'application/json',
      };
      console.log(config, 'config');
    },
  ],
  // 返回的数据格式，做一下处理
  responseInterceptors: [
    async (res) => {
      let result = await res.json();
      console.log('result', result.results);
      return { code: 0, data: result.results };
    },
  ],
};
export async function getInitialState() {
  const data = { isLogin: '', userMsg: null };
  return data;
}
export const layout = ({ initialState }) => {
  return {
    onPageChange: () => {
      const { isLogin, userMsg } = initialState;

      const { location } = history;
      const token = localStorage.getItem('token');
      if (!token && location.pathname != '/login') {
        history.push('/login');
      }
      if (token && location.pathname == '/login') {
        history.push('/carousel/list');
      }
      // 如果没有登录，重定向到 login
      // if (!currentUser && location.pathname !== '/user/login') {
      //   history.push('/user/login');
      // }
    },
    rightRender: () => {
      return (
        <div style={{ backgroundColor: '#ccc', width: '200px' }}>66umi</div>
      );
    },
  };
};
